package com.steamedfish.auctionitemserver.service.impl;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.steamedfish.auctionitemserver.entiry.AuctionItemImage;
import com.steamedfish.auctionitemserver.mapper.AuctionItemImageMapper;
import com.steamedfish.auctionitemserver.service.AuctionItemImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 高山福
 * @since 2021-03-18
 */
@Service
public class AuctionItemImageServiceImpl extends ServiceImpl<AuctionItemImageMapper, AuctionItemImage> implements AuctionItemImageService {

    @Autowired
    private COSClient cosClient;


    private String PWD_KEY_PREFIX = "images/";
    private String ROOT_PWD = "https://auction-item-image-1301464063.cos.ap-nanjing.myqcloud.com/";


    @Override
    public List<String> upload(MultipartFile multipartFile) throws IOException {



        // 指定要上传的文件
        File localFile = transferToFile(multipartFile);

        // 指定文件将要存放的存储桶
        String bucketName = "auction-item-image-1301464063";
        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String prefix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf(".") + 1);
        String key = PWD_KEY_PREFIX + UUID.randomUUID() + "." + prefix;


        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);





//        Gson gson=new Gson();
//        String json=gson.toJson(putObjectResult);
//        System.out.println(json);

        List<String> list = new ArrayList<>();
        String url = ROOT_PWD + key;
        list.add(url);

        return list;
    }


    private File transferToFile(MultipartFile multipartFile) {
        //选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            System.out.println(originalFilename+"===");
            String filename = originalFilename.substring(0, originalFilename.indexOf("."));
            filename = filename.replace(",", "0");
            filename = filename.replace("&", "1");
            filename = filename.replace("=", "3");
            String prefix = originalFilename.substring(originalFilename.indexOf(".")+1);
            System.out.println(filename+prefix);
            if (filename.length() <= 4) {
                filename = filename + UUID.randomUUID();
            }
            file=File.createTempFile(filename, prefix);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
